package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Window.Type;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.swing.Action;

public class create_game extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	private final Action action = new SwingAction();
	private final Action action_1 = new SwingAction_1();
	private JCheckBox chckbxEnable;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					create_game frame = new create_game();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public create_game() {
		setTitle("Create Game");
		setType(Type.UTILITY);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 331);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblCreateGame = new JLabel("Create Game");
		lblCreateGame.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		JLabel lblGameName = new JLabel("Name of Game");
		
		JLabel lblBriefDescrition = new JLabel("Brief Descrition");
		
		JLabel lblEnablePassword = new JLabel("Enable Password");
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel lblRetypepassword = new JLabel("Retype Password");
		lblRetypepassword.setHorizontalAlignment(SwingConstants.RIGHT);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		passwordField = new JPasswordField();
		
		passwordField_1 = new JPasswordField();
		
		 chckbxEnable = new JCheckBox("Enable");
		
		JButton btnNewButton = new JButton("Create Game");
		btnNewButton.setAction(action_1);
		
		JButton btnCloseWindow = new JButton("Close Window");
		btnCloseWindow.setAction(action);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblPassword, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblEnablePassword)
								.addComponent(lblBriefDescrition)
								.addComponent(lblGameName)
								.addComponent(lblCreateGame, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblRetypepassword, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(textField, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)
								.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)
								.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
								.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
								.addComponent(chckbxEnable)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(58)
							.addComponent(btnNewButton)
							.addGap(18)
							.addComponent(btnCloseWindow, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(74, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblCreateGame)
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGameName)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBriefDescrition)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEnablePassword)
						.addComponent(chckbxEnable))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPassword)
						.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblRetypepassword)
						.addComponent(passwordField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnCloseWindow))
					.addContainerGap())
		);
		contentPane.setLayout(gl_contentPane);
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Close Window");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}
	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "Create Game");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			
			if (!textField.getText().isEmpty()) {
				if (!textField_1.getText().isEmpty()) {
			Connection conn = connect.connect_DB();		
			try {
				int ID=0;
				int pass=0;
				  String query = "SELECT MAX(ID) FROM games";
				    PreparedStatement ps = conn.prepareStatement(query);
				    ResultSet rs = ps.executeQuery();
				    while ( rs.next() ) {
				    ID=rs.getInt(1);	
				    }
				    rs.close();
					ps.close();

					
			if (chckbxEnable.isSelected()) {
				//With Pass
				if (!passwordField.getText().equals(passwordField_1.getText())) { 
					pass=1;
					
					
				} else {
					 JOptionPane.showMessageDialog( null,
		       		         "Passwords do not match!",
		       		         "Error", JOptionPane.ERROR_MESSAGE);
				}
			} 

			Timestamp ts = new Timestamp(1);
			     query = "INSERT INTO games (ID,Game_Name,Game_Description,pass,passwd,rep_win,rep_imp,rep_dist,rep_ret) VALUES (?,?,?,?,?,?,?,?,?)";
			     ps = conn.prepareStatement(query);
			     ps.setInt(1, ID+1);
			     ps.setString(2,textField.getText());
			     ps.setString(3,textField_1.getText());
			     ps.setInt(4, pass);
			     ps.setString(5,passwordField.getText());
			     ps.setTimestamp(6, ts);
			     ps.setTimestamp(7, ts);
			     ps.setTimestamp(8, ts);
			     ps.setTimestamp(9, ts);
			     ps.execute();
			     ps.close();
			     
			     values.setIDGame(ID+1);
			     values.setNGame(textField.getText());
			     // Insert Status
			     query = "INSERT INTO game_status (Game_ID, Week, winery, importer, distributor, retailer, admin) VALUES (?,?,?,?,?,?,?)";
			     ps = conn.prepareStatement(query);
			     ps.setInt(1, ID+1);
			     ps.setInt(2, 0);
			     ps.setInt(3, 0);
			     ps.setInt(4, 0);
			     ps.setInt(5, 0);
			     ps.setInt(6, 0);
			     ps.setInt(7, 0);
			     ps.execute();
			     ps.close();
			     
			     // Insert Previous Inventory and Back Orders.
			     for (int lev=0;lev<4;lev++) {
			    	 for (int wee=-4;wee<1;wee++) {
					     query = "INSERT INTO orders_inv_back (Game_ID,Level,Semana,ava_sem,Red_Ship,Red_inv,Red_back,White_Ship,White_Inv,White_Back) VALUES (?,?,?,?,?,?,?,?,?,?)";
					     ps = conn.prepareStatement(query);
					     ps.setInt(1, ID+1);
					     ps.setInt(2,lev);
					     ps.setInt(3,wee);
					     if (lev==0) {
					     ps.setInt(4, wee+4);
					     } else {
					    	 ps.setInt(4, wee+2); 
					     }
					     ps.setInt(5,4);
					     ps.setInt(6,12);
					     ps.setInt(7,0);
					     ps.setInt(8, 4);
					     ps.setInt(9,12);
					     ps.setInt(10,0);
					     ps.execute();
					     ps.close();		 
			    	 }
			     }
			     //Set Orders
			     for (int lev=0;lev<=4;lev++) {
			    	 for (int wee=-4;wee<1;wee++) {
					     query = "INSERT INTO orders (Game_ID,Level,Semana,Order_red,Order_White) VALUES (?,?,?,?,?)";
					     ps = conn.prepareStatement(query);
					     ps.setInt(1, ID+1);
					     ps.setInt(2,lev);
					     ps.setInt(3,wee);
					     ps.setInt(4, 4);
					     ps.setInt(5,4);
					     ps.execute();
					     ps.close();		 
			    	 }
			     } 
			     
			     
			     for (int lev=0;lev<4;lev++) {
			    	 for (int wee=-4;wee<1;wee++) {
					     query = "INSERT INTO shipping (Game_ID,Level,Semana,Ship_red,Ship_White,units) VALUES (?,?,?,?,?,?)";
					     ps = conn.prepareStatement(query);
					     ps.setInt(1, ID+1);
					     ps.setInt(2,lev);
					     ps.setInt(3,wee);
					     ps.setInt(4, 4);
					     ps.setInt(5,4);
					     if (lev==0) {
					    	 ps.setInt(6,2); 
					     } else {
					    	 ps.setInt(6,4); 
					     }
					     
					     ps.execute();
					     ps.close();		 
			    	 }
			     } 
			     for (int wee=-4;wee<1;wee++) {
				     query = "INSERT INTO production (Game_ID,Semana,Bot_red,Bot_White) VALUES (?,?,?,?)";
				     ps = conn.prepareStatement(query);
				     ps.setInt(1, ID+1);
				     ps.setInt(2,wee);
				     ps.setInt(3, 4);
				     ps.setInt(4,4);
				     ps.execute();
				     ps.close();		 
		    	 }
			     conn.close();
			     
			     JOptionPane.showMessageDialog( null,
	       		         "Game Created Succesfully!",
	       		         "OK", JOptionPane.INFORMATION_MESSAGE);
			     dispose();
			} catch (SQLException i) {
				// TODO Auto-generated catch block
				i.printStackTrace();
			}
			
			} else {
				 JOptionPane.showMessageDialog( null,
	       		         "Please enter a brief description!",
	       		         "Error", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			 JOptionPane.showMessageDialog( null,
       		         "Please enter a Game Name !",
       		         "Error", JOptionPane.ERROR_MESSAGE);
		}
		
			
			
		}
	}
}
